const db = require('../db/index')
// const bcryptjs = require('bcryptjs')
// const jwt = require('jsonwebtoken')
// const config = require('../config')
const TYPE = require("../constants")

exports.getList = (req, res) => {
    const { page, limit, type } = req.body
    db.getConnection((err, connection) => {
        if (err) {
            return res.send({ status: 1, message: err.message })
        }
        let sqlStr = 'select * from user limit ?,?'
        let params = [(parseInt(page) - 1) * parseInt(limit), parseInt(limit)]
        if (type && type.length) {
            // if (!type.includes(TYPE)) res.send({ status: 1, message: "type字段不合法" })
            sqlStr = 'select * from user where type=? limit ?,?'
            params = [type, (parseInt(page) - 1) * parseInt(limit), parseInt(limit)]
        }
        connection.query(sqlStr, params, (err, results) => {
            res.send(results)
        })
    })
}

exports.getInfo = (req, res) => {
    const { id } = req.params
    db.getConnection((err, connection) => {
        if (err) {
            return res.send({ status: 1, message: err.message })
        }
        const sqlStr = 'select * from user where id=?'
        connection.query(sqlStr, id, (err, results) => {
            res.send(results)
        })
    })
}
